package com.gome.ocean.service.authority.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.gome.ocean.dao.authority.GSystemUpdateLogDAO;
import com.gome.ocean.dao.model.authority.GSystemUpdateLog;
import com.gome.ocean.service.authority.SystemUpdateLogService;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * @author liyuefeng
 */
@Service("SystemUpdateLogService")
public class SystemUpdateLogServiceImpl implements SystemUpdateLogService {

    private static final Logger LOG = LoggerFactory.getLogger(SystemUpdateLogServiceImpl.class);

    @Autowired
    private GSystemUpdateLogDAO gSystemUpdateLogDAO;

    @Override
    public boolean deleteByPrimaryKey(Long id) {
        return gSystemUpdateLogDAO.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean insertSelective(GSystemUpdateLog record) {
        return gSystemUpdateLogDAO.insertSelective(record) > 0;
    }

    @Override
    public GSystemUpdateLog selectByPrimaryKey(Long id) {
        return gSystemUpdateLogDAO.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateByPrimaryKeySelective(GSystemUpdateLog record) {
        return gSystemUpdateLogDAO.updateByPrimaryKeySelective(record) > 0;
    }

    @Override
    public boolean updateAll() {
        return gSystemUpdateLogDAO.updateAll() > 0;
    }

    @Override
    public List<GSystemUpdateLog> findLatelySysLogByStatusAndScope(Map<String, Object> param) {
        return gSystemUpdateLogDAO.findSysLogByLatelyTime(param);
    }

    @Override
    public List<GSystemUpdateLog> getLogByGroup(Map<String, Object> param) {
        return gSystemUpdateLogDAO.getLogByGroup(param);
    }

    @Override
    public Page<GSystemUpdateLog> getList(int pageNum, int pageSize) {
        return (Page<GSystemUpdateLog>) gSystemUpdateLogDAO.getList(new RowBounds(pageNum, pageSize));
    }

    @Override
    public String findMaxTimeByStatusAndScope(Map<String, Object> param) {

        return gSystemUpdateLogDAO.findMaxTimeByStatusAndScope(param);
    }

    @Override
    public Map<String, Object> getAllSysLogByMap(Map<String, Object> param) {
        Map<String, Object> map = Maps.newHashMap();
        try {
            List<GSystemUpdateLog> sysLists = gSystemUpdateLogDAO.getAllSysLogByMap(param);
            if (sysLists != null && sysLists.size() > 0) {
                Set<String> addTimeSets = Sets.newLinkedHashSet();
                Map<String, Object> setMap = Maps.newLinkedHashMap();
                for (GSystemUpdateLog sys : sysLists) {
                    addTimeSets.add(sys.getAddTimeNode());
                }
                for (String addTime : addTimeSets) {
                    param.put("addTimeNode", addTime);
                    List<GSystemUpdateLog> sysListByTime = gSystemUpdateLogDAO.getAllSysLogByMap(param);
                    setMap.put(addTime, sysListByTime);
                }
                map.put("addTimeSets", addTimeSets);
                map.put("resultMaps", setMap);
                return map;
            }
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
        return map;
    }

}
